home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Tools & Apps / Networking & Communications / Serial NB Sample Driver / Test / glue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  7.8 KB  |  392 lines  |  [TEXT/MPS ]

  1. /********************************************************************************/
  2. /*                                                                                */
  3. /*        glue.c - A/ROSE driver serial glue calls.                                */
  4. /*                                                                                */
  5. /*        Richard W. Mincher.        February 20, 1990.                                */
  6. /*                                                                                */
  7. /*        Copyright © 1990, Apple Computer, Inc.  All rights reserved.            */
  8. /*                                                                                */
  9. /********************************************************************************/
  10.  
  11.  
  12. #include    "Types.h"
  13. #include    "Files.h"
  14. #include    "Memory.h"
  15. #include    "OSUtils.h"
  16. #include    "Resources.h"
  17. #include    "Serial.h"
  18.  
  19. #include    "AROSE.h"
  20. #include    "os.h"
  21. #include    "managers.h"
  22. #include    "Download.h"
  23.  
  24. #include    "ARDriver.h"
  25. #include    "ARTask.h"
  26.  
  27. void
  28.     RxComplete(AROSEmessage *m);
  29. pascal OSErr
  30.     ARPBControl(ParmBlkPtr pb, short async);
  31. pascal OSErr
  32.     ARPBStatus(ParmBlkPtr pb, short async);
  33. pascal OSErr
  34.     ARPBWrite(ParmBlkPtr pb, short async);
  35. pascal OSErr
  36.     ARPBRead(ParmBlkPtr pb, short async);
  37.  
  38. pascal OSErr
  39. AROpenDriver(char *name, short *refNum)
  40. {
  41.     ParamBlockRec    pb;
  42.     AROSEmessage    *m;
  43.     short            slot, port;
  44.     tid_type        tid, nmtid;
  45.     struct ST_PB     stpb;
  46.     short            smask;
  47.     long            rtype;
  48.     char            fname[65];
  49.     short            namelen;
  50.     SysEnvRec        se;
  51.     short            rfNum, saverf, i;
  52.         
  53. #ifdef    DEBUG
  54.     Debugger();
  55. #endif    DEBUG
  56.     
  57.     *refNum = 0;
  58.     SysEnvirons( curSysEnvVers, &se );
  59.     
  60.     if (OpenQueue( 0 ) == 0)
  61.     {
  62. #ifdef    DEBUG
  63.         Debugger();
  64. #endif    DEBUG
  65.         return (-26);  /*    ERROR - couldn't open queue    */
  66.     }
  67.  
  68. //
  69. //    Get file name.  Read up to ":".
  70. //
  71.  
  72.     i = name[0];
  73.     for(namelen = 1; namelen < i; namelen++)
  74.     {
  75.         if (name[namelen] == ':')
  76.             break;
  77.         fname[namelen-1] = name[namelen];
  78.     }
  79.     
  80.     if (namelen == i)
  81.     {
  82. #ifdef    DEBUG
  83.         Debugger();
  84. #endif    DEBUG
  85.         CloseQueue();
  86.         return (-26);  /*    ERROR - Couldn't parse filename    */
  87.     }
  88.     
  89.     fname[namelen-1] = 0;
  90.  
  91. //
  92. //    Get resource name.
  93. //
  94.  
  95.     for(i=0; i < 4; i++)
  96.         ((char *)&rtype)[i] = name[namelen+1+i];
  97.     
  98. //
  99. //    Get slot number and port.
  100. //
  101.  
  102.     slot = -1;
  103.     port = -1;
  104.     if (name[namelen+5] == '9')
  105.         slot = 9;
  106.     else if ((name[namelen+5] >= 'A') && (name[namelen+5] <= 'E'))
  107.         slot = name[namelen+5] - 'A' + 10;
  108.     else if ((name[namelen+5] >= 'a') && (name[namelen+5] <= 'e'))
  109.         slot = name[namelen+5] - 'a' + 10;
  110.     
  111.     if ((name[namelen+6] >= '0') && (name[namelen+6] <= '9'))
  112.         port = name[namelen+6] - '0';
  113.     
  114. //
  115. //    Validate port and slot by checking to see if the card is a Livonia card and
  116. //    if the driver for that port is already running.
  117. //
  118.     
  119.     saverf = CurResFile();
  120.     c2pstr( fname );
  121.     rfNum = OpenRFPerm( fname, se.sysVRefNum, fsRdPerm );
  122.     if (rfNum == -1)
  123.     {
  124. #ifdef    DEBUG
  125.         Debugger();
  126. #endif    DEBUG
  127.         CloseQueue();
  128.         return (-26);  /*    ERROR - Couldn't open file    */
  129.     }
  130.     
  131.     if ((port == -1) || (slot == -1) || (GetResource(rtype, 0) == 0))
  132.     {
  133.         CloseResFile( rfNum );
  134.         UseResFile( saverf );
  135. #ifdef    DEBUG
  136.         Debugger();
  137. #endif    DEBUG
  138.         CloseQueue();
  139.         return (-26);  /*    ERROR - invalid Slot/Port    */
  140.     }
  141.     
  142.     smask = 1 << slot;
  143.     (void)NewFindcard( &smask, 0 );
  144.     if (!smask)
  145.     {
  146.         CloseResFile( rfNum );
  147.         UseResFile( saverf );
  148. #ifdef    DEBUG
  149.         Debugger();
  150. #endif    DEBUG
  151.         CloseQueue();
  152.         return (-26);  /*    ERROR - not MCP card    */
  153.     }
  154.     
  155.     stpb.stack = 4096;
  156.     stpb.heap = 0;
  157.     stpb.priority = 28;
  158.     stpb.ParentTID = 0;
  159.     stpb.InitRegs.D_Registers[7] = port;
  160.     
  161. //
  162. //    Try to do dynamic download of driver.  If it fails we probably have to boot
  163. //    the card.
  164. //
  165.  
  166.     tid = DynamicDownload(slot, rtype, &stpb, name, name[0]+1 );
  167.     if (!tid)
  168.     {
  169. //
  170. //    Download the generic AROSE stuff to the card.  We'll get the name manager tid.
  171. //    We should do this ONLY if AROSE is not already running.
  172. //
  173.  
  174.         nmtid = StartAROSE( slot );
  175.         if (!nmtid)
  176.         {
  177.             CloseResFile( rfNum );
  178.             UseResFile( saverf );
  179. #ifdef    DEBUG
  180.             Debugger();
  181. #endif    DEBUG
  182.             CloseQueue();
  183.             return (-26);  /*    ERROR - AROSE not started    */
  184.         }
  185.     
  186. //
  187. //    Now we download the serial driver to the card.  D7 will be set up to point to
  188. //    the port number.
  189. //
  190.     
  191.         tid = DynamicDownload(slot, rtype, &stpb, name, name[0] + 1);
  192.         if (!tid)
  193.         {
  194.             CloseResFile( rfNum );
  195.             UseResFile( saverf );
  196. #ifdef    DEBUG
  197.             Debugger();
  198. #endif    DEBUG
  199.             CloseQueue();
  200.             return (-26);  /*    ERROR - Serial driver not loaded    */
  201.         }
  202.     }
  203.  
  204.     CloseResFile( rfNum );
  205.     UseResFile( saverf );
  206.  
  207. //
  208. //    Hit task with hammer to wake it up and open the driver.
  209. //
  210.  
  211.     m = GetMsg();
  212.     m->mTo = tid;
  213.     m->mCode = SNBOpen;
  214.     m->mSData[0] = (unsigned long)&pb;
  215.     pb.ioParam.ioResult = 1;
  216.     pb.ioParam.ioCompletion = 0;
  217.     Send( m );
  218.     (void)Receive( 0, 0, 0, 0, (ProcPtr)StripAddress((Ptr)RxComplete) );
  219.     while(pb.ioParam.ioResult > 0)
  220.         PushAIPC();
  221.     *refNum = (slot << 12) + (port << 7);
  222.     return (pb.ioParam.ioResult);
  223. }
  224.  
  225. pascal OSErr
  226. ARCloseDriver(short    refNum)
  227. {
  228. #pragma unused(refNum)
  229.     ParamBlockRec    pb;
  230.     AROSEmessage    *m;
  231.     struct globals    *g;
  232.     
  233. #ifdef    DEBUG
  234.     Debugger();
  235. #endif    DEBUG
  236.     g = (struct globals *)(((refNum & 0xF000) << 8) +
  237.         (refNum & 0x0180) + 0x200);
  238.     SetMyTid(g->hisTid);
  239.     m = GetMsg();
  240.     m->mTo = (tid_type)(g->myTid);
  241.     m->mFrom = (tid_type)(g->hisTid);
  242.     m->mCode = SNBClose;
  243.     m->mSData[0] = (unsigned long)&pb;
  244.     pb.ioParam.ioResult = 0;
  245.     pb.ioParam.ioCompletion = 0;
  246.     Send( m );
  247.     CloseQueue();
  248.     return (pb.ioParam.ioResult);
  249. }
  250.  
  251. pascal OSErr
  252. ARKillIO(short refNum)
  253. {
  254. #pragma unused(refNum)
  255.     ParamBlockRec    pb;
  256.     
  257. #ifdef    DEBUG
  258.     Debugger();
  259. #endif    DEBUG
  260.     pb.cntrlParam.ioResult = 1;
  261.     pb.cntrlParam.ioCompletion = 0;
  262.     pb.cntrlParam.csCode = 1;
  263.     pb.cntrlParam.ioCRefNum = refNum;
  264.     return (ARPBControl( &pb, 0 ));
  265. }
  266.  
  267. pascal OSErr
  268. ARSerReset(short refNum, short serConfig)
  269. {
  270. #pragma unused(refNum)
  271.     ParamBlockRec    pb;
  272.     
  273. #ifdef    DEBUG
  274.     Debugger();
  275. #endif    DEBUG
  276.     pb.cntrlParam.ioResult = 1;
  277.     pb.cntrlParam.ioCompletion = 0;
  278.     pb.cntrlParam.csCode = 8;
  279.     pb.cntrlParam.csParam[0] = serConfig;
  280.     pb.cntrlParam.ioCRefNum = refNum;
  281.     return (ARPBControl( &pb, 0 ));
  282. }
  283.  
  284. pascal OSErr
  285. ARSerSetBuf(short refNum, char *serBPtr, short serBLen)
  286. {
  287. #pragma unused(refNum)
  288.     ParamBlockRec    pb;
  289.     
  290. #ifdef    DEBUG
  291.     Debugger();
  292. #endif    DEBUG
  293.     pb.cntrlParam.ioResult = 1;
  294.     pb.cntrlParam.ioCompletion = 0;
  295.     pb.cntrlParam.csCode = 9;
  296.     pb.cntrlParam.csParam[0] = ((unsigned long)serBPtr >> 16) & 0xFFFF;
  297.     pb.cntrlParam.csParam[1] = (unsigned long)serBPtr & 0xFFFF;
  298.     pb.cntrlParam.csParam[2] = serBLen;
  299.     pb.cntrlParam.ioCRefNum = refNum;
  300.     return (ARPBControl( &pb, 0 ));
  301. }
  302.  
  303. pascal OSErr
  304. ARSerHShake(short refNum, SerShk *flags)
  305. {
  306. #pragma unused(refNum)
  307.     ParamBlockRec    pb;
  308.     short            i;
  309.     
  310. #ifdef    DEBUG
  311.     Debugger();
  312. #endif    DEBUG
  313.     pb.cntrlParam.ioResult = 1;
  314.     pb.cntrlParam.ioCompletion = 0;
  315.     pb.cntrlParam.csCode = 10;
  316.     pb.cntrlParam.ioCRefNum = refNum;
  317.     for(i=0; i < sizeof(SerShk); i++)
  318.         ((char *)(pb.cntrlParam.csParam))[i] = ((char *)flags)[i];
  319.     return (ARPBControl( &pb, 0 ));
  320. }
  321.  
  322. pascal OSErr
  323. ARSerSetBrk(short refNum)
  324. {
  325. #pragma unused(refNum)
  326.     ParamBlockRec    pb;
  327.     
  328. #ifdef    DEBUG
  329.     Debugger();
  330. #endif    DEBUG
  331.     pb.cntrlParam.ioResult = 1;
  332.     pb.cntrlParam.ioCompletion = 0;
  333.     pb.cntrlParam.csCode = 12;
  334.     pb.cntrlParam.ioCRefNum = refNum;
  335.     return (ARPBControl( &pb, 0 ));
  336. }
  337.  
  338. pascal OSErr
  339. ARSerClrBrk(short refNum)
  340. {
  341. #pragma unused(refNum)
  342.     ParamBlockRec    pb;
  343.     
  344. #ifdef    DEBUG
  345.     Debugger();
  346. #endif    DEBUG
  347.     pb.cntrlParam.ioResult = 1;
  348.     pb.cntrlParam.ioCompletion = 0;
  349.     pb.cntrlParam.csCode = 11;
  350.     pb.cntrlParam.ioCRefNum = refNum;
  351.     return (ARPBControl( &pb, 0 ));
  352. }
  353.  
  354. pascal OSErr
  355. ARSerGetBuf(short refNum, long *count)
  356. {
  357. #pragma unused(refNum)
  358.     ParamBlockRec    pb;
  359.     
  360. #ifdef    DEBUG
  361.     Debugger();
  362. #endif    DEBUG
  363.     pb.cntrlParam.ioResult = 1;
  364.     pb.cntrlParam.ioCompletion = 0;
  365.     pb.cntrlParam.csCode = 2;
  366.     pb.cntrlParam.ioCRefNum = refNum;
  367.     (void)ARPBStatus( &pb, 0 );
  368.     *count = ((pb.cntrlParam.csParam[0] << 16) | pb.cntrlParam.csParam[1]);
  369.     return (pb.ioParam.ioResult);
  370. }
  371.  
  372. pascal OSErr
  373. ARSerStatus(short refNum, SerStaRec *serSta)
  374. {
  375. #pragma unused(refNum)
  376.     ParamBlockRec    pb;
  377.     short            i;
  378.     
  379. #ifdef    DEBUG
  380.     Debugger();
  381. #endif    DEBUG
  382.     pb.cntrlParam.ioResult = 1;
  383.     pb.cntrlParam.ioCompletion = 0;
  384.     pb.cntrlParam.csCode = 8;
  385.     pb.cntrlParam.ioCRefNum = refNum;
  386.     (void)ARPBStatus( &pb, 0 );
  387.     for(i=0; i < sizeof(SerStaRec); i++)
  388.         ((char *)serSta)[i] = ((char *)(pb.cntrlParam.csParam))[i];
  389.     return (pb.ioParam.ioResult);
  390. }
  391.  
  392.